package nl.hu.verbrandendephoenix.shared;

import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;

public class FieldVerifier {
	
	private static FieldVerifier fv;
	
	private FieldVerifier() { }
	
	public static FieldVerifier get() {
		if(fv == null) {
			fv = new FieldVerifier();
		}
		return fv;
	}
	
	public void addRequired(TextBox tb) {
		tb.addStyleName("required");
	}
	
	public void removeRequired(TextBox tb) {
		tb.removeStyleName("required");
	}
	
	public void addRequired(ListBox lb) {
		lb.addStyleName("required");
	}
	
	public void removeRequired(ListBox lb) {
		lb.removeStyleName("required");
	}
	
	public boolean checkString(int min, int max, String string) {	
		int length = string.length();
		
		if(length < min || length > max) {
			return false;
		} else {
			return true;
		}
	}
	
	public boolean checkAlphaNumeric(String alpha) {
		return alpha.matches("[A-Za-z]+");
	}
	
	public boolean checkNumbers(String numbers) {
		return numbers.matches("[0-9]+");
	}
	
	public boolean checkDouble(String dnumbers) {
		return dnumbers.matches("[0-9.]+");
	}
	
	public boolean checkEmailAddress(String email) {
		return email.matches("^[A-Za-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$");
	}
	
	public boolean checkPhoneNumber(String phone) {
		return phone.matches("([0-9]{10})");
	}
	
	public boolean checkAddress(String address) {
		return address.matches("[A-Za-z]+(\\s)?[0-9]+");
	}
	
	public boolean checkPassword(String password) {
		return password.matches("^[A-Za-z0-9]{4,20}$");
	}
	
	public boolean checkZipcode(String zipcode) {
		return zipcode.matches("^[1-9][0-9]{3}[A-Z]{2}$");
	}
}
